import useUserList from '@/hooks/useUserList';
import { getList as getOrgList, getTreeList as getOrgTreeList } from '@/services/system/org';
import { getList as getRoleList } from '@/services/system/role';
import { getApiData, getSelectList } from '@/util/util';
import { ProColumns } from '@ant-design/pro-components';

type DefaultColumnType =
  | 'index'
  | 'seq'
  | 'createTime'
  | 'updateTime'
  | 'operationId'
  | 'searchCreateTime'
  | 'searchUpdateTime'
  | 'org'
  | 'searchOrg'
  | 'role'
  | 'title'
  | 'directorId'
  | 'mobile';

const useDefaultColumn = () => {
  const { userList } = useUserList();

  const user: ProColumns = {
    title: '操作人',
    dataIndex: 'operationId',
    valueType: 'select',
    hideInSearch: true,
    fieldProps: {
      showSearch: true,
      options: userList,
    },
  };

  const defaultColumn: {
    [k in DefaultColumnType]: ProColumns;
  } = {
    mobile: {
      title: '手机号码',
      dataIndex: 'mobile',
    },
    title: {
      title: '名称',
      dataIndex: 'title',
    },
    role: {
      title: '角色',
      dataIndex: 'roleId',
      valueType: 'select',
      fieldProps: { showSearch: true },
      request: () =>
        getSelectList<ROLE_TYPE.Info>({
          label: 'title',
          value: 'id',
          server: getRoleList,
        }),
    },
    org: {
      title: '机构',
      dataIndex: 'orgId',
      valueType: 'select',
      hideInSearch: true,
      key: 'orgList',
      request: () =>
        getSelectList<ORG_TYPE.Info>({
          label: 'title',
          value: 'id',
          server: getOrgList,
        }),
    },
    searchOrg: {
      title: '机构',
      dataIndex: 'orgId',
      valueType: 'treeSelect',
      hideInTable: true,
      request: () => getApiData(getOrgTreeList),
      fieldProps: {
        fieldNames: {
          value: 'id',
          label: 'title',
          children: 'children',
        },
      },
    },
    index: {
      title: '序号',
      dataIndex: 'index',
      valueType: 'index',
    },
    createTime: {
      title: '创建时间',
      dataIndex: 'createTime',
      hideInSearch: true,
      sorter: true,
    },
    searchCreateTime: {
      title: '创建时间',
      dataIndex: 'createTime',
      valueType: 'dateRange',
      hidden: true,
      search: {
        transform: (value) => {
          return {
            startTime: value[0],
            endTime: value[1],
          };
        },
      },
    },
    updateTime: {
      title: '更新时间',
      dataIndex: 'updateTime',
      hideInSearch: true,
      sorter: true,
    },
    searchUpdateTime: {
      title: '更新时间',
      dataIndex: 'updateTime',
      valueType: 'dateRange',
      hidden: true,
      search: {
        transform: (value) => {
          return {
            updateStartTime: value[0],
            updateEndTime: value[1],
          };
        },
      },
    },
    seq: {
      title: '排序',
      dataIndex: 'seq',
      hideInSearch: true,
      sorter: true,
    },
    operationId: { ...user },
    directorId: {
      ...user,
      title: '负责人',
      dataIndex: 'directorId',
    },
  };
  return defaultColumn;
};
export default useDefaultColumn;
